<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="渠道名称" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择" clearable>
          <el-option
            v-for="dict in channelList"
            :key="dict.channelCode"
            :label="dict.channelName"
            :value="dict.channelCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生码状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择生码状态" clearable>
          <el-option
            v-for="dict in bus_code_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="consumeState">
        <el-select v-model="queryParams.consumeState" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in qr_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker 
          :clearable="false"
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-10px flex items-center">
      <div class="total">
        <span>二维码申请总数：</span>
        <span class="amount">{{ formatNumberWithCommas(summary.total) }}</span>
      </div>
      <div class="total">
        <span>二维码申请成功数：</span>
        <span class="amount">{{ formatNumberWithCommas(summary.success) }}</span>
      </div>
      <div class="total">
        <span>二维码申请失败数：</span>
        <span class="amount">{{ formatNumberWithCommas(summary.error) }}</span>
      </div>
    </div>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['bus:platQrRecord:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="platQrRecordList">
        <el-table-column label="渠道名称" min-width="140" align="center" prop="businessChannel" />
        <el-table-column label="二维码ID" width="220" align="center" prop="requestId" show-overflow-tooltip />
        <el-table-column label="生码状态" width="90" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="bus_code_status" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" width="90" align="center" prop="consumeState">
          <template #default="scope">
            <dict-tag :options="qr_order_status" :value="scope.row.consumeState"/>
          </template>
        </el-table-column>
        <el-table-column label="渠道用户ID" width="210" align="center" prop="partnerId" show-overflow-tooltip />
        <el-table-column label="用户编号" width="180" align="center" prop="platformUserId"  show-overflow-tooltip/>
        <el-table-column label="申请时间" width="160" align="center" prop="codeApplyTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.codeApplyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="生码时间" width="160" align="center" prop="codeCreateTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.codeCreateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="扣费时间" width="160" align="center" prop="consumeTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.consumeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="生码失败原因" width="200" align="center" prop="errMsg" show-overflow-tooltip />
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="PlatQrRecord">
import { listPlatQrRecord, getQrRecordStatistic } from "@/api/bus/platQrRecord"
import { listChannel } from "@/api/channel/platchannelinfo"

const { proxy } = getCurrentInstance()
const { bus_channel_code, bus_code_status, qr_order_status, bus_qr_code_type } = proxy.useDict('bus_channel_code', 'bus_code_status', 'qr_order_status', 'bus_qr_code_type')

const channelList = ref([])
const platQrRecordList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const searchDate = ref([])
const summary = ref({})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    codeType: null,
    channelCode: null,
    state: null,
    consumeState: null,
    platformUserId: null,
    partnerId: null,
    mobile: null,
    beginDate: null,
    endDate: null
  }
})

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginDate = e[0]
    queryParams.value.endDate = e[1]
  } else {
    queryParams.value.beginDate = null
    queryParams.value.endDate = null
  }
  handleQuery()
}

// 渠道列表
const getChannelList = () => {
  const params = {
    pageNum: 1,
    pageSize: 1000
  }
  listChannel(params).then(response => {
    channelList.value = response.rows
  })
}

/** 查询二维码生码记录列表 */
function getList() {
  loading.value = true
  listPlatQrRecord(queryParams.value).then(response => {
    platQrRecordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
  getQrRecordStatistic(queryParams.value).then(res => {
    summary.value = res.data || {}
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  const currentDate = proxy.parseTime(new Date(), '{y}-{m}-{d}')
  queryParams.value.beginDate = currentDate
  queryParams.value.endDate = currentDate
  searchDate.value = [currentDate, currentDate]
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bus/platQrRecord/export', {
    ...queryParams.value
  }, `二维码生码记录_${proxy.getFileImportName()}.xlsx`)
}

onMounted(() => {
  const currentDate = proxy.parseTime(new Date(), '{y}-{m}-{d}')
  queryParams.value.beginDate = currentDate
  queryParams.value.endDate = currentDate
  searchDate.value = [currentDate, currentDate]
  getChannelList()
  getList()
})

</script>

<style lang="scss" scoped>
.total {
  width: 33.33%;
  color: #606266;
  font-size: 14px;
  padding: 8px 0;

  .amount {
    color: #333;
    font-size: 18px;
    padding-right: 3px;
    font-weight: bold;
  }
}
</style>
